﻿using UnityEngine;
using DG.Tweening;
using BToolkit;
using System.Collections.Generic;
using UnityEngine.UI;

namespace BToolkit.ExeUtils
{
    public class FadeOutOnIdle : MonoBehaviour
    {
        public enum Method { Alpha, Scale }
        public Method method = Method.Alpha;
        public RectTransform[] objs;

        class Obj
        {
            public RectTransform trans;
            public float defaultA;
            public Vector3 defaultScale;
        }
        List<Obj>[] _objsArr;

        float timer, wait = 5;
        bool isTriggerFadeOut;

        private void Awake()
        {
            wait = 5;// Config.Instance.BtnFadeOutWait;
            _objsArr = new List<Obj>[objs.Length];
            for (int i = 0; i < _objsArr.Length; i++)
            {
                _objsArr[i] = new List<Obj>();
                RectTransform[] transes = objs[i].GetComponentsInChildren<RectTransform>();
                for (int j = 0; j < transes.Length; j++)
                {
                    Obj obj = new Obj();
                    obj.trans = transes[j];
                    obj.defaultA = obj.trans.GetComponent<MaskableGraphic>().color.a;
                    obj.defaultScale = obj.trans.localScale;
                    _objsArr[i].Add(obj);
                }
            }
        }

        void Update()
        {
            if (!isTriggerFadeOut)
            {
                timer += Time.deltaTime;
                if (timer >= wait)
                {
                    isTriggerFadeOut = true;
                    for (int i = 0; i < objs.Length; i++)
                    {
                        objs[i].GetComponentInChildren<BButton>().enabled = false;
                    }
                    for (int i = 0; i < _objsArr.Length; i++)
                    {
                        List<Obj> objs = _objsArr[i];
                        for (int j = 0; j < objs.Count; j++)
                        {
                            objs[j].trans.DOKill(false);
                            if (method == Method.Alpha)
                            {
                                objs[j].trans.DOAlpha(0, 2, Ease.Linear, false);
                            }
                            else if (method == Method.Scale)
                            {
                                objs[j].trans.DOScale(Vector3.zero, 1);
                            }
                        }
                    }
                }
            }
            if (isTriggerFadeOut && Input.GetMouseButtonDown(0))
            {
                timer = 0;
                isTriggerFadeOut = false;
                for (int i = 0; i < _objsArr.Length; i++)
                {
                    List<Obj> objs = _objsArr[i];
                    for (int j = 0; j < objs.Count; j++)
                    {
                        objs[j].trans.DOKill(false);
                        if (method == Method.Alpha)
                        {
                            objs[j].trans.DOAlpha(objs[j].defaultA, false);
                        }
                        else if (method == Method.Scale)
                        {
                            objs[j].trans.DOScale(objs[j].defaultScale, 0.3f).SetEase(Ease.OutBack);
                        }
                    }
                }
                for (int i = 0; i < objs.Length; i++)
                {
                    objs[i].GetComponentInChildren<BButton>().enabled = true;
                }
            }
        }
    }
}